<?php

namespace Mlym\CodeGeneration\Middleware\Api;

use EasySwoole\HttpClient\HttpClient;
use Exception;

class Api
{
    protected $config;

    public function __construct(Config $config)
    {
        $this->config = $config;
    }

    /**
     * @throws Exception
     */
    public function request()
    {
        $client = new HttpClient($this->config->getUrl());
        $client->setQuery([
            'project' => $this->config->getProject(),
            'module' => $this->config->getModule()
        ]);
        $client->setTimeout(5);
        $client->setConnectTimeout(10);
        $client->setContentType($client::CONTENT_TYPE_APPLICATION_JSON);
        $result = $client->get();
        $result = $result->json(true);

        if (empty($result)) {
            throw new Exception('The middleware platform requested invalid data');
        }
        return $result;

    }
}